{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Small exmaple of Contextual Loss (CX)\n",
    "[Open with Colab](https://colab.research.google.com/github/S-aiueo32/contextual_loss_pytorch/blob/master/doc/small_example.ipynb)\n",
    "\n",
    "I will show you a small example of CX here.\n",
    "\n",
    "## Calculate CX\n",
    "For the first, import dependencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create images called $x$ and $y$ in the paper.\n",
    "You can see the pixels whose spatial correspondings have similar looks.\n",
    "For simplicity, I will use the RGB values as the features through this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f982aee6dd0>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAABWCAYAAADMpy0uAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAHDElEQVR4nO3dX4gdZx3G8efpJmlIU2NstK5paCtJi1XR2rUgxT/0D229aApaaC8khZZVJKg3YkSQ0hujFy0IvQk2UEXaShVcSyAobSlBLVlLrU1CmjUXbUI0bVISo2i68niRIxzWs93amXNmzrzfDyw7c+Zl3t+c/Z2Hs7Mze5xEAIDuO6/pAgAAo0HgA0AhCHwAKASBDwCFIPABoBAEPgAUolLg236P7V/bPtT7vnaRcf+2/ULva6bKnMAo0NvoIle5Dt/2DySdTLLd9jZJa5N8a8C4M0lWV6gTGCl6G11UNfAPSvpckmO2JyU9k+TKAe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      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.array([\n",
    "              [231, 76, 60],\n",
    "              [46, 204, 113],\n",
    "              [52, 152, 219],\n",
    "]).reshape(1, 3, 3) / 255\n",
    "y = np.array([\n",
    "              [245, 183, 177],\n",
    "              [171, 235, 198],\n",
    "              [174, 214, 241],\n",
    "]).reshape(1, 3, 3) / 255\n",
    "\n",
    "plt.figure()\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.imshow(x)\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.imshow(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Along with the key idea, convert $x$, $y$ to $X$, $Y$ respectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = x.reshape(-1, 3)\n",
    "Y = y.reshape(-1, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate cosine distances between all points."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# normalize\n",
    "mu = Y.mean(axis=0, keepdims=True)\n",
    "X_centered = X -mu\n",
    "Y_centered = Y -mu\n",
    "X_normalized = X_centered / np.linalg.norm(X_centered, ord=2, axis=1, keepdims=True)\n",
    "Y_normalized = Y_centered / np.linalg.norm(Y_centered, ord=2, axis=1, keepdims=True)\n",
    "\n",
    "# cosine distance\n",
    "d = 1 - np.matmul(X_normalized, Y_normalized.transpose())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looking at the heatmap of `d`, you confirm the correspondings are similar."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUgAAAEXCAYAAADPzN0RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/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pOS/jtVC/c/Fo5pXNxC9XdmaprrVUDPaa6DUE+9/TzwDresS1BSQ0xFvqdRmDYtSNuvt31Yh+1q4GdTtxn1zw1d6lhf/7wXuI7q/4aDsh5obX3tVx/rWEbSjsBuDHdWUGNMtjfafrL+eCHw8iHG06Sf73BkXGCaa9PUWwp8R9vzdOBpkyAbLAfeXO+/Gbi6vYCkPSTtXO/PpZrFM8hl1VYCCyQdJGknqkGY9pHy1jhPAL7lupd7SBpjauu/Op6qj6mU5cAp9UjtUcCjLV0nIzfqaa71tXpOvWXE31E/MY36exqp0qNEg9io+vG+CdwDfAPYsz6+ELiw3v8dYDXVSO5q4LQhxHEs1SjfD4D31cfOBo6v93cBPgesBb4HHDyC76Yppn8A1tTfy7XAfxhiLJdRrf/5NFXf2WnA6cDp9XkB59exrgYWDvm7aYrnjJbv5kbgd4Ycz6uoFtm+Dbi13o4t/B31E9NIv6dRbplqGBHRxUy5xY6IGLgkyIiILpIgIyK6SIKMiOgiCTIiooskyIiILpIgY+xIer6kK0vHEZHnICMiukgLMkZG0tmS3tHy+YNdFmA9sNsithGjlAQZo3QRcAqApB2o5oZ/umhEET1Mm+XOYvqz/UNJGyW9lGrNzn+zPTMWNYgZKQkyRu1Cqtc87EPVoowYWxmkiZGql11bDcwGFtie6FDmQKp3nBw22ugifl1akDFStp+SdC3wSKfk2Fp0VDFFdJMEGSNVD84cBfxRj2J7MUYv04rtV0axY2QkHUq1WPA3bd/TpcxCqoVsPzbK2CI6SR9kFCPpJcClbYeftH1kiXgi2iVBRkR0kVvsiIgukiAjIrpIgoyI6CIJMiKii/8HxD0znaHgHtwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(d)\n",
    "plt.colorbar()\n",
    "plt.title('Distance Map');\n",
    "plt.ylabel('x_i');\n",
    "plt.xlabel('y_j');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Along with Eq.(2) in the paper, normalize using the minimum.\n",
    "Through this process, the most similar point over $y_j$ becomes `1.`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAT4AAAEXCAYAAAA0myjOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAbmUlEQVR4nO3de7QlZX3m8e9D04ByhzaA0NAyQ0wIyQTsgLcwHVFHkEBmhUxQB4Sl0+KI0Vk6DuoSEiasKIk6sHDEFlBuQRAjtogXDBC8QWiwpWku0hAZumlpaO4qlz7nmT/qPc1ms8/Z1c3euw6nng+r1q7Le6p+u+jzO+9bb71Vsk1ERJts0nQAERGjlsQXEa2TxBcRrZPEFxGtk8QXEa2TxBcRrZPE11KS/lrSBWV+d0lPSJo14GP8QtIbX8DPv0PS9wYZUwQk8Q1N+aVfI2nLjnXvlnRNg2H1ZPv/2d7K9tiojinpy5KelvR4mW6R9HeStu2I60Lbb665r78dbsQbTpLLv4FNO9bNLutyA22DkviGaxbwgRe6E1Vm4v+rU21vDbwMOBZ4NfCjzj8WM8DDwMEdyweXddGgmfjLNJ38PfBhSdv12ijptZJukPRo+Xxtx7ZrJJ0i6UfAr4E9y7q/lfTj0jT9pqQdJV0o6bGyj3kd+zhN0r1l242S/niSOOaV2smmkl5T9j0xPSnpF6XcJpJOkHSXpLWSLpG0Q8d+jpJ0T9n28bonyfaTtm8ADgN2pEqCSDpG0g/LvCR9ttSWHpO0TNI+khYC7wA+MnFOSvmJOB+XdKuk/9wR5zGSfijpHyQ9LOnfJB3csX0HSV+SdF/ZflnHtkMlLZX0SPn/8Ad9vt75wNEdy0cD53Wd/2Ml3VZivVvSez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aepGROsk8UVE6yTxRUTrJPFFROv8f9PWvVHkWD8FAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "d_tilde = d / (d.min(axis=1, keepdims=True) + 1e-5)\n",
    "\n",
    "plt.imshow(d_tilde)\n",
    "plt.colorbar()\n",
    "plt.title('Normalized Distance Map');\n",
    "plt.ylabel('x_i');\n",
    "plt.xlabel('y_j');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Convert to similarity showed in Eq.(3)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUgAAAEXCAYAAADPzN0RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAaVUlEQVR4nO3de9RddX3n8feHCGQQEDAdQQhExyim6BRMCRQvtN4CdYhrqZ3QVsSFk/FCxwu2pTqDLZ2ZKjPjjI4sMVWGi5SL6MJHJ64IioOtBomICYEiAbVcolxNQBHI83zmj70fejjZ55LknPM7z5PPa629ztln/85vf58DfPnt/bts2SYiIra1W+kAIiLGVRJkREQHSZARER0kQUZEdJAEGRHRQRJkREQHSZC7MEl/JOnrO/jdV0i6rWX/J5JesxOxPCrp+Tv6/YhhSIKc5SS9XNJ3JG2W9JCkf5D02wC2L7H9uh2p1/a3bb9oUHHa3tv2nXXMF0j6zztaV52sn5A0r+3zH0iypAU7F23sKpIgZzFJ+wJfBf43cABwMPBXwOMl42ol6RlDqvrHwMkt53kJsNeQzhWzVBLk7PZCANuX2p60/Zjtr9teByDpVEl/P124bl29W9Ltkh6R9NeS/lXdAt0i6QpJe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WBPkc25vq9z8DntOh3FxJayWtkTToJNo01an9X6SnTXUCpqc6DUs/MQG8qb5cu1LS/Ibjo9JvvKN0rKQfSvqapN8c1Um7TL0t9hv1Mx141L/TsJUeB9k3SdcABzYc+nDrjm1L6jR26TDb90h6PvBNSett3zHoWGeYrwCX2n5c0r+nauH+XuGYxkVf01wHrcfU2yIGMR14JpoxLUjbr7F9RMP2ZeDn05cZ9et9Heq4p369E/gW1f8NB2UoU52GHZPtB20/Xu9+FnjZEOPppZ/fcGRcYJprr6m3FPiNduXpwDMmQfbQOoXobcCX2wtI2l/SnvX7eVSzeNrXndwZQ5nqNOyY2u5fnUR1j6mUCeCUuqf2GGBzy62TkdOIp7nW5+o69ZYR/0b9xDTq32mkSvcSDWKjuo/3DeB24BrggPrzxcBn6/e/A6yn6sldD5w2hDhOpOrluwP4cP3Z2cBJ9fu5wBeAjcD3gOeP4LfpFdPfABvq3+Va4PAhxnIpsAl4kure2WnAO4F31sdFtejpHfU/o8VD/m16xXN6y2+zBvidIcfzcqpFttcBN9XbiYV/o35iGunvNMotUw0jIjqYLZfYEREDlwQZEdFBEmRERAdJkBERHSRBRkR0kAQZEdFBEmSMHUnPlXRl6TgiMg4yIqKDtCBjZCSdLel9Lfv/pcMCrAs6LWIbMUpJkDFK5wOnAEjajWpu+OeLRhTRxYxZ7ixmPts/kfSgpCOp1uz8ge3ZsahBzEpJkDFqn6V6zMOBVC3KiLGVTpoYqXrZtfXA7sBC25MNZRZQPePkiNFGF/F0aUHGSNl+QtK1wC+akmNr0VHFFNFJEmSMVN05cwzwli7Fns0YPUwrdl3pxY6RkbSIarHgb9i+vUOZxVQL2X5ilLFFNMk9yChG0kuAi9s+ftz2khLxRLRLgoyI6CCX2BERHSRBRkR0kAQZEdFBEmRERAf/HyfBk5lQwXMgAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "w = np.exp((1 - d_tilde) / 0.1)\n",
    "\n",
    "plt.imshow(w)\n",
    "plt.colorbar()\n",
    "plt.title('Similarity Map');\n",
    "plt.ylabel('x_i');\n",
    "plt.xlabel('y_j');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The bandwidth parameter is set as `0.1` above.\n",
    "The setting is very important to get the acceptable result i.e. the inappropriate value prevents Top-1 feature enhancement like below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUgAAAEXCAYAAADPzN0RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAdbElEQVR4nO3dfbQddX3v8fcn4SEI4UFjBSEQrLGKaCtGHsQHWlEgeolrqV1gFbDYFJWqLXpL9V60tLbIbblXLiwxIlfgWpQHF0YbL6KCaJWHgDEhUCREKAlRIEAIyFPO+dw/Zg7d7MycvXOy956dk89rrVlnz57fnvnuSc73/Ob3+81vZJuIiNjYlKYDiIgYVkmQERE1kiAjImokQUZE1EiCjIiokQQZEVEjCbIPJP2JpO9N8LNvlHRHy/rdkg7fjFgek/SSiX5+GEg6QtKVLeuHSrqz/G7v7PDZnp7PYSbpRkmvbDqOySQJcoIkvUHSTyWtk/SQpH+T9DoA21+z/baJ7Nf2j23/Xq/itL2T7ZVlzF+V9PcT3VeZXJ6WNKPt/Z9LsqRZmxdtrc8BZ7Ssnw6cU363K2s+A/T+fA65f6I4N9EjSZATIGln4DvA/waeD+wJ/C3wVJNxtZK0TZ92/Svg2JbjvAp4Xp+ORflHZxfb17e8vQ+wvF/HrImjX+ezlxYCfyhp96YDmSySICfmZQC2L7E9YvsJ29+zvRRA0gmSfjJWuKxdfbi8LFwv6e8k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XELxf+ZsdtcrwTGvc21F8pbb68APu6WeQma1CGmRs7TIGwxNUjbh9vev2L5FvCbscuM8uf9NftYXf5cCVxL8dewV1YDrbWvvcr3KstI2gbYhf7eFdQxJttrbT9Vrp4PvLaP8XTSzTkcGDdwm2unW29p4BxtzbcDbzEJsoOFwPHl6+OBb7UXkLSbpO3L1zMo7uLp5bRqNwGzJe0raTuKTpj2nvLWON8N/NBlK3efdIyprf3qaIo2pqYsBI4re2oPBta1NJ0M3KBvcy2PNe6ttwz4HHUT06DP00A13UvUi4WiHe8HwJ3A94Hnl+/PAc4vX78eWEbRk7sMOLEPccyl6OW7C/h0+d7pwNHl62nAZcAK4EbgJQM4N51i+kdgeXlergFe3sdYLqGY//MZirazE4GTgJPK7QLOLWNdBszp87npFM/JLefmeuD1fY7nDRSTbC8FlpTL3IbPUTcxDfQ8DXLJrYYRETUmyyV2RETPJUFGRNRIgoyIqJEEGRFRIwkyIqJGEmRERI0kyBg6kl4s6fKm44jIOMiIiBqpQcbASDpd0sdb1j9XMwHrrLpJbCMGKQkyBukC4DgASVMo7g3/v41GFDGOLWa6s9jy2b5b0lpJr6GYs/PntifHpAYxKSVBxqCdT/GYh90papQRQyudNDFQ5bRry4Btgdm2RyrKzKJ4xsn+g40u4rlSg4yBsv20pGuAR6qSY2vRQcUUUScJMgaq7Jw5GHjPOMVewBA9TCu2XunFjoGRtB/FZME/sH1nTZk5FBPZfmGQsUVUSRtkNEbSq4CL295+yvZBTcQT0S4JMiKiRi6xIyJqJEFGRNRIgoyIqJEEGRFR4/8DxZQEAZbZnpsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "w_ = np.exp((1 - d_tilde) / 0.8)\n",
    "\n",
    "plt.imshow(w_)\n",
    "plt.colorbar()\n",
    "plt.title('Similarity Map (failure case)');\n",
    "plt.ylabel('x_i');\n",
    "plt.xlabel('y_j');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Normalize and get final result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CX: 0.9878605414928291\n"
     ]
    }
   ],
   "source": [
    "cx_ij = w / np.sum(w, axis=1, keepdims=True)  # normalize\n",
    "cx = np.mean(np.max(cx_ij, axis=0))\n",
    "print(f'CX: {cx}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Robustness of CX\n",
    "CX is the metric invariant misalignment of images.\n",
    "To show the robustness, shuffle the pixels of $y$ and measure CX on all patterns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[trial 0]: 0.9878605414928291\n",
      "[trial 1]: 0.9878605414928291\n",
      "[trial 2]: 0.9878605414928291\n",
      "[trial 3]: 0.9878605414928291\n",
      "[trial 4]: 0.9878605414928291\n",
      "[trial 5]: 0.9878605414928291\n"
     ]
    }
   ],
   "source": [
    "from itertools import permutations\n",
    "\n",
    "def compute_cx(x, y):  # integrate as a function\n",
    "    X = x.reshape(-1, 3)\n",
    "    Y = y.reshape(-1, 3)\n",
    "\n",
    "    mu = Y.mean(axis=0, keepdims=True)\n",
    "    X_centered = X -mu\n",
    "    Y_centered = Y -mu\n",
    "    X_normalized = X_centered / np.linalg.norm(X_centered, ord=2, axis=1, keepdims=True)\n",
    "    Y_normalized = Y_centered / np.linalg.norm(Y_centered, ord=2, axis=1, keepdims=True)\n",
    "\n",
    "    d = 1 - np.matmul(X_normalized, Y_normalized.transpose())\n",
    "    d_tilde = d / (d.min(axis=1, keepdims=True) + 1e-5)\n",
    "    w = np.exp((1 - d_tilde) / 0.1)\n",
    "    cx_ij = w / np.sum(w, axis=1, keepdims=True)\n",
    "\n",
    "    return np.mean(np.max(cx_ij, axis=0))\n",
    "\n",
    "for i, p in enumerate(permutations([0, 1, 2])):\n",
    "    y_ = y[:, p, :]\n",
    "    cx = compute_cx(x, y_)\n",
    "    print(f'[trial {i}]: {cx}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Did you cnofirm all of CXs is same? It's robustness!\n",
    "\n",
    "That's all, happy CX!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
